package com.sofwin.system.controller;

import com.sofwin.common.ResponseEntry;
import com.sofwin.pojo.SysMenu;
import com.sofwin.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @packageName: com.sofwin.system
 * @user: andyliu
 * @date: 2021/12/17 9:49
 * @email 115176513@qq.com
 * @description: TODO
 */
@Controller
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @GetMapping("/index")
    public String index(){
        return "menu/index";
    }
    @PostMapping("/data")
    @ResponseBody
    public ResponseEntry data(SysMenu menu){
        List<SysMenu> data = menuService.selectMenus(menu);
        return ResponseEntry.success(data);
    }


    @GetMapping("/treeData")
    @ResponseBody
    public ResponseEntry queryAll(Integer roleId){
        List<SysMenu> menus = menuService.queryAllMenus(roleId);
        return ResponseEntry.success(menus);
    }

    @GetMapping("toEdit")
    public String toEdit(Integer id, Model model,Integer parentId){
        if(id!=null){
            SysMenu menu = menuService.selectById(id);
            model.addAttribute("menu",menu);
        }
        if(parentId!=null){
            SysMenu parentMenu = menuService.selectById(parentId);
            model.addAttribute("parentMenu",parentMenu);

        }
        return "menu/edit";
    }
    @GetMapping("treeView")
    public String treeView(){
        return "menu/tree";
    }
    @PostMapping("save")
    @ResponseBody
    public ResponseEntry save(SysMenu menu){
        boolean status = menuService.saveOrUpdate(menu);
        if(status)return ResponseEntry.success("保存成功");
        return ResponseEntry.error("保存失败");
    }
}
